# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=enchant
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.6.1
pkgrel=1
pkgdesc="Enchanting Spell Checking Library (mingw-w64)"
arch=('any')
url="https://abiword.github.io/enchant/"
license=(LGPL2.1)
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=(${MINGW_PACKAGE_PREFIX}-gcc-libs
         ${MINGW_PACKAGE_PREFIX}-glib2
         ${MINGW_PACKAGE_PREFIX}-aspell
         ${MINGW_PACKAGE_PREFIX}-hunspell)
options=('!libtool' 'strip' '!debug' '!makeflags')
source=("https://github.com/AbiWord/${_realname}/releases/download/${_realname}-${pkgver//./-}/${_realname}-${pkgver}.tar.gz"
        01-relocate.patch
        01-not-use-hardcoded-paths.patch
        02-build-fixes.patch)
sha256sums=('bef0d9c0fef2e4e8746956b68e4d6c6641f6b85bd2908d91731efb68eba9e3f5'
            'b245913e77b77352d3015b938fb213bef9422152a1466b290804a86bdc2aee1c'
            '131c9f90f6a77c5e9117ebd141e9a36cae0d964463557b94524c7f1ce892dd71'
            '1d73dc36d49bd6bf3996ae0315039ff2af0454f80118c973c9ace99deb98f5d4')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  # True relocate patch
  #patch -p1 -i ${srcdir}/01-relocate.patch
  # Use instead relocate patch
  patch -p1 -i ${srcdir}/01-not-use-hardcoded-paths.patch

  # See https://git.archlinux.org/svntogit/packages.git/commit/?h=packages/enchant&id=7d223aa
  patch -p1 -i ${srcdir}/02-build-fixes.patch

  autoreconf -fiv
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  ../${_realname}-${pkgver}/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --enable-ispell \
    --enable-myspell \
    --enable-aspell \
    --with-system-myspell \
    --with-aspell-prefix=${MINGW_PREFIX} \
    --disable-voikko \
    --disable-uspell \
    --disable-hspell \
    --disable-zemberek

  # MSYS2_ARG_CONV_EXCL="-DENCHANT_USPELL_DICT_DIR=;-DENCHANT_MYSPELL_DICT_DIR=;-DENCHANT_ISPELL_DICT_DIR=;-DENCHANT_PREFIX_DIR=;-DENCHANT_GLOBAL_MODULE_DIR=;-DENCHANT_GLOBAL_ORDERING=;-DENCHANT_BIN_DIR="
  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  # MSYS2_ARG_CONV_EXCL="-DENCHANT_USPELL_DICT_DIR=;-DENCHANT_MYSPELL_DICT_DIR=;-DENCHANT_ISPELL_DICT_DIR=;-DENCHANT_PREFIX_DIR=;-DENCHANT_GLOBAL_MODULE_DIR=;-DENCHANT_GLOBAL_ORDERING=;-DENCHANT_BIN_DIR="
  make DESTDIR="${pkgdir}" install
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING.LIB" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LIB"
}
